package com.aote.model.block;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.tools.BSCalendar;

/**
 * 多层日期天数显示model，可指定每层的日期显示格式。 可指定开始结束月份,日期,以当前月为0,前一个月为-1,后一月为1 在改model所在元素配置
 * startmonth='-1' startday='22' endmonth='0',endday='21'
 */
public class MutexDayDateBlockModel implements MultiLevelTableHeadModel {
	/**
	 * 组件
	 */
	protected HTMLBasicComponent component = null;

	/**
	 * 开始日期
	 */
	private BSCalendar startCalendar = BSCalendar.now();

	/**
	 * 结束日期
	 */
	private BSCalendar endCalendar = BSCalendar.now();

	public void init(HttpServletRequest request) throws Exception {
		// 设置日期
		setDate();
	}

	/**
	 * 设置日期
	 */
	private void setDate() {
		// 得到开始月，开始天，结束月，结束天
		int startMonth = Integer.parseInt(this.component.getElement()
				.getAttribute("startMonth"));
		int endMonth = Integer.parseInt(this.component.getElement()
				.getAttribute("endMonth"));
		int startDay = Integer.parseInt(this.component.getElement()
				.getAttribute("startDay"));
		int endDay = Integer.parseInt(this.component.getElement().getAttribute(
				"endDay"));
		// 设置日期
		this.startCalendar.setMonth(this.startCalendar.getMonth() + startMonth);	
		this.endCalendar.setMonth(this.endCalendar.getMonth() + endMonth);
		this.startCalendar.setDay(startDay);
		this.endCalendar.setDay(endDay);
	}

	public void setComponent(HTMLBasicComponent component) {
		this.component = component;
	}

	public void modifyCellValue(Element td, Document doc, int row, int col) {
	}

	public String getType(int col) {
		return null;
	}

	public void updatePageElement(HTMLBasicComponent c) throws Exception {
	}

	public void service(HttpServletRequest request, HttpServletResponse response)
			throws Exception {
	}

	public int getColumnCount(int level) {
		return this.endCalendar.getDayDispersion(this.startCalendar) + 1 ;
	}

	public String getColumnName(int level, int i) {
		String result = "";
		if (level == 0) {
			result = getNumberDay(i) + "";
		} else if (level == 1) {
			result = getChinaDay(i);
		}
		return result;
	}

	/**
	 * 得到数字日期列明
	 */
	private int getNumberDay(int col) {
		int result = 0;
		int startDay = this.startCalendar.getDay();
		if ((startDay + col) > this.startCalendar.getDaysOfMonth()) {
			result = col
					- (this.startCalendar.getEndDayOfMonth().getDay() - startDay);
		} else {
			result = startDay + col;
		}
		return result;

	}

	/**
	 * 得到中文日期显示
	 */
	private String getChinaDay(int col) {
		String result = "";
		int startDay = this.startCalendar.getDay();
		if ((startDay + col) > this.startCalendar.getDaysOfMonth()) {
			int day = col
					- (this.startCalendar.getEndDayOfMonth().getDay() - startDay);
			BSCalendar temp = (BSCalendar) this.endCalendar.clone();
			temp.setDay(day);
			result = temp.getChinaNumberOfWeek(temp.getDayOfWeek() - 1);
		} else {
			int day = startDay + col;
			BSCalendar temp = (BSCalendar) this.startCalendar.clone();
			temp.setDay(day);
			result = temp.getChinaNumberOfWeek(temp.getDayOfWeek() - 1);
		}
		return result;
	}

	public int getColumnCount() {
		return this.endCalendar.getDayDispersion(this.startCalendar) + 1 ;
	}

	public String getColumnName(int index) {

		return null;
	}

	public int getRowCount() {

		return 0;
	}

	public void setCondition(String condition) {

	}

}
